import request from '@/config/axios'

export interface AudioResourceCreateReqVO {
  title: string
  subtitle?: string
  audioUrl?: string
  cover?: string
  duration?: number
  category: string
  language?: string
  bookId?: number
  bookSetId?: number
  sortOrder?: number
  enabled?: boolean
}

export interface AudioResourceUpdateReqVO extends AudioResourceCreateReqVO {
  id: number
}

export interface AudioResourceAdminRespVO {
  id: number
  title: string
  subtitle?: string
  audioUrl?: string
  cover?: string
  duration?: number
  category: string
  language?: string
  bookId?: number
  bookSetId?: number
  bookTitle?: string
  bookSetTitle?: string
  sortOrder?: number
  enabled: boolean
  createTime: string
  updateTime: string
  creator?: string
  updater?: string
}

export interface AudioResourcePageReqVO extends PageParam {
  title?: string
  category?: string
  language?: string
  bookId?: number
  bookSetId?: number
  enabled?: boolean
}

// 创建音频资源
export const createAudioResource = (data: AudioResourceCreateReqVO) => {
  return request.post({
    url: '/reading/admin/audio/create',
    data
  })
}

// 更新音频资源
export const updateAudioResource = (data: AudioResourceUpdateReqVO) => {
  return request.put({
    url: '/reading/admin/audio/update',
    data
  })
}

// 删除音频资源
export const deleteAudioResource = (id: number) => {
  return request.delete({
    url: '/reading/admin/audio/delete',
    params: { id }
  })
}

// 获取音频资源详情
export const getAudioResource = (id: number) => {
  return request.get({
    url: '/reading/admin/audio/get',
    params: { id }
  })
}

// 获取音频资源分页
export const getAudioResourcePage = (params: AudioResourcePageReqVO) => {
  return request.get({
    url: '/reading/admin/audio/page',
    params
  })
}

// 上传音频文件
export const uploadAudioFile = (data: FormData) => {
  return request.post({
    url: '/reading/audio-file/resource',
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 上传音频封面
export const uploadAudioCover = (audioId: number, data: FormData) => {
  return request.post({
    url: `/reading/audio-file/audio/${audioId}/cover`,
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 获取绘本集列表（用于选择器）
export const getBookSetList = (params?: { language?: string; title?: string }) => {
  return request.get({
    url: '/reading/admin/book-sets/list',
    params
  })
}

// 获取绘本列表（用于选择器）
export const getBookList = (params?: { bookSetId?: number; title?: string }) => {
  return request.get({
    url: '/reading/admin/books/list',
    params
  })
}

// 获取下一个音频资源ID
export const getNextAudioResourceId = () => {
  return request.get({
    url: '/reading/audio-file/next-id'
  })
}
